package com.example.lib_net

import com.example.lib_net.interceptor.SignInterceptor
import com.example.lib_net.interceptor.TokenInterceptor
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Qualifier
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class NetModel {
    @Provides
    @Singleton
    @CommonOkHttpClient
    fun provideOkHttpClient():OkHttpClient{
        return OkHttpClient.Builder()
            .readTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .addInterceptor(TokenInterceptor())
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    }
    @Provides
    @Singleton
    @SignOkHttpClient
    fun provideSignOkHttpClient():OkHttpClient{
        return OkHttpClient.Builder()
            .readTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .addInterceptor(TokenInterceptor())
            .addInterceptor(SignInterceptor())
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    }
    @Provides
    @Singleton
    @CommonRetrofit
    fun provideRetrofit(@CommonOkHttpClient client:OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl("http://10.161.9.80:8886")
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
    }
    @Provides
    @Singleton
    @SignRetrofit
    fun provideSignRetrofit(@SignOkHttpClient client:OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl("http://10.161.9.80:8886")
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
    }
    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class CommonOkHttpClient
    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class SignOkHttpClient
    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class CommonRetrofit
    @Qualifier
    @Retention(AnnotationRetention.RUNTIME)
    annotation class SignRetrofit
}